package com.document;

import java.io.File;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.DBHelper.MySQLIteOpenHelper;
//主界面 实现程序的入口 主要显示 浏览文件 新建文件 导入文件 我的文件  
public class MainActivity extends MyActivity {
  static  Handler handler;
private ImageButton SearchFile;
private ImageButton InputFile;
private ImageButton NewFile;
private ImageButton MyFile;
private ImageButton changSkin;
ProgressDialog progressdialog;


public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	SearchFile=(ImageButton)findViewById(R.id.search);
	
	NewFile=(ImageButton)findViewById(R.id.new1);
	MyFile=(ImageButton)findViewById(R.id.my);
	InputFile=(ImageButton)findViewById(R.id.input);
	changSkin=(ImageButton)findViewById(R.id.change);	
	SearchFile.setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
		Intent i=new Intent();
		i.setClass(MainActivity.this, MainBrowserActivity.class);
		startActivity(i);
		finish();		
		}
	});	
     // 用Handler来更新UI 
	handler = new Handler(){  
  
        @Override  
        public void handleMessage(Message msg) {  
              
            //关闭ProgressDialog  
            progressdialog.dismiss();             
            //更新UI  
            Toast.makeText(getApplicationContext(), "导入成功...", Toast.LENGTH_LONG).show();
            Intent i=new Intent();
            i.setClass(MainActivity.this, MyFile.class);
            startActivity(i);
            
        }};  
        InputFile.setOnClickListener(new OnClickListener() {
        	MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
        	//删除上次插入数据       	
			public void onClick(View v) {
				 helper.delete("delete from ListFile");
				 //显示ProgressDialog  
				progressdialog = ProgressDialog.show(MainActivity.this, "正在导入...", "请稍后...", true, false);  
				progressdialog.setCancelable(true);
                //新开线程处理扫描文件
                new Thread( new Runnable() {
					public void run() {
							 readFile(); 							
			}
				}).start(); 			
			}
		});

	NewFile.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			Intent i=new Intent();
			i.setClass(MainActivity.this, SelectCreatWay.class);
			startActivity(i);
			finish();			
		}
	});
	MyFile.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			Intent i=new Intent();
			i.setClass(MainActivity.this, MyFile.class);
			startActivity(i);
			finish();			
		}
	});
	changSkin.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
	
		Intent i=new Intent();
		i.setClass(getApplicationContext(), MyActivity.class);
		startActivity(i);
		finish();
		}
	});	
}
private void readFile() {
    File file = Environment.getExternalStorageDirectory();
    File[] files = file.listFiles();
    readFile(files);
    //向handler发消息  
    handler.sendEmptyMessage(0);  
}
private void readFile(final File[] files){
	MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
    if(files!=null && files.length>0){
        for(int i=0;i<files.length;i++){
            if(files[i].isDirectory()){
                final int tag=i;
                 readFile(files[tag].listFiles());
            }else{
               
            	//	取得文件的长度
            	long length = files[i].length();
            	if(getFile(files[i].getAbsolutePath())&& length>0){
        			 helper=new MySQLIteOpenHelper(getApplicationContext());
					String insertsql="insert into ListFile values('"+files[i].getAbsolutePath()+"')";
					helper.insert(insertsql);	
                System.out.println("name"+files[i].getAbsolutePath());
            }
            }
            if(i==files.length-1){               
            }
        }
    }
   
	helper.close();
}
private boolean getFile(String fName) {
	boolean re;
	//取得文件拓展名
	String end=fName.substring(fName.lastIndexOf(".")+1,
	fName.length()).toLowerCase();
	//根据拓展名类型决定打开方式
	if(end.equals("doc")||end.equals("xls")||end.equals("pdf")||end.equals("txt")||end.equals("html"))
	{
		re=true;
	}
	else{
		re=false;
	}
	return re;
}
//实现再按一次返回退出程序
private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){   
        if((System.currentTimeMillis()-exitTime) > 2000){  
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                
            exitTime = System.currentTimeMillis();   
        } else {
            finish();
            System.exit(0);
        }
        return true;   
    }
    return super.onKeyDown(keyCode, event);
}

}
